home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c,comp.unix.programmer,comp.databases
- Subject: Re: Aligning struct fields with NDBM (was: Casting unsigned short ...)
- Date: Mon, 08 Jan 96 23:01:11 GMT
- Organization: none
- Message-ID: <821142071snz@genesis.demon.co.uk>
- References: <simmons.820857453@rzdspc1> <simmons.821116795@rzdspc1>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <simmons.821116795@rzdspc1>
- simmons@rzdspc1.informatik.uni-hamburg.de "Geoffrey Simmons" writes:
-
- >Thanks to everyone who responded publicly and privately to my post
- >"Casting unsigned short as unsigned int -> Bus error" on comp.lang.c
- >and gnu.gcc.help. As it turns out, my problem has nothing to do with
- >the business about casting, and I suspect it has to do with aligning
- >the struct fields of data fetched by the Unix NDBM database library.
- >Thus it is probably not a C language problem, which is why I've the changed
- >the Subject:, Newsgroups:, and Followup-To: lines.
-
- While ndbm isn't part of the C language, your problem seems very much C
- language related so I've reinstated comp.lang.c.
-
- > #include <ndbm.h>
-
- For the purposes of comp.lang.c assume ndbm.h declares (among other
- things) the function dbm_fetch() and the type datum as:
-
- typedef struct {
- char *dptr;
- int dsize;
- } datum;
-
- > typedef struct {
- > char str1[10];
- > char str2[10];
- > unsigned char c;
- > unsigned short short1;
- > unsigned short short2;
- > time_t mytime;
- > } RecType;
- >
- > RecType *MyRec;
- > datum aKey, theDatum;
-
- Assume datum is defined by:
-
- > char key[] = "theKey";
- >
- > aKey.dptr = &key[0];
- > aKey.dsize = strlen(key) + 1;
-
- Did you #include <string.h>?
-
- > theDatum = dbm_fetch(dataFile, aKey);
- > if (theDatum.dptr == NULL)
- > {
- > fprintf(stderr, "Data not found\n");
- > exit(1); /* No error here */
- > }
- > if ((MyRec = (RecType *) malloc(theDatum.dsize)) == NULL)
-
- You might as well malloc(sizeof(RecType)) since if theDatum.dsize is not
- this value then your code breaks. You might want to test that theDatum.dsize
- has the correct value.
-
- Did you include stdlib.h or otherwise ensure a declaration for malloc
- was in scope? It is a bad idea to cast the return type of malloc in C
- since it prevents the compiler warning you about this sort of error.
-
- If you don't have a declaraion in scope the compiler assumes that malloc
- returns int. An int->RecType * conversion may require a completely different
- operation to a void *->RecType * conversion.
-
- > {
- > fprintf(stderr, "Error allocting memory\n");
- > exit(1); /* No error here */
- > }
- > MyRec = (RecType *) theDatum.dptr;
-
- You've just gone and thrown away the pointer to your malloc'd block (this is
- a memory leak). Maybe you meant to copy the data in the struct in which case
- you need instead:
-
- memcpy(MyRec, theDatum.dptr, sizeof(RecType));
-
- Don't use:
-
- *MyRec = *(RecType *) theDatum.dptr;
-
- because this may assume alignment that is not guaranteed.
-
- ...
-
- >But when I try to dereference any of the integer-type members, I get bus
- >errors:
- >
- > unsigned short myshort;
- >
- > myshort = MyRec->short1; /* Bus error! */
- > myshort = MyRec->short2; /* Bus error! */
- > printf("mytime = %ul\n", MyRec->mytime); /* Bus error! */
- >
- >I conclude from all this that memory allocation for the struct is not the
- >problem, but suspect that the NDBM fetch is not aligning data in memory
- >properly (there are warnings about this in the comp.lang.c FAQ and the
- >free-databases FAQ).
-
- That is not surprising since you were trying to access ndbm's internal
- datastructures which know nothing of your alignment requirements. memcpy
- to an object with correct alignment as I've noted above.
-
- >Is this a well-known problem with NDBM, with a well-known solution? I sure
- >hope so.
-
- There is nothing in your post that indicates a problem with ndbm, you
- just need to fix your code.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-